import React,{Component} from 'react'
import {getObject} from '../../config/public'
import Pages from '../Pages'
import {Link} from 'react-router'
import { DatePicker,Spin,message} from 'antd';
import moment from 'moment';
import $ from 'jquery'
import {Urls} from '../../config/urls'
import * as pub from '../../config/public';
class PointManage extends Component {
	constructor(args) {
		super()
		this.state = {
			pointList : getObject(1),
			activeIndex : 0,
			start:'',
			listUserIntgral:[],
			totalPage:'',
			loading:false,
			startTime:moment(new Date(new Date()-1000*60*60*24*7), 'YYYY/MM/DD'),
			endTime:moment(new Date(),'YYYY/MM/DD'),
			status:false,
			changePage:undefined
		}	
	}

	componentWillMount(){
		let _this = this
		this.setState({loading:true})
		let _data = pub.behavior({
				start:this.state.start,	
		})
		$.ajax({
			type:"POST",
			url:Urls.USERINTGRAL,
			data:_data,
			success:function(data){
				_this.setState({listUserIntgral:data.listUserIntgral?data.listUserIntgral:[]})	
				_this.setState({
					loading:false,
					totalPage:data.total
				})
			},
			error:function(data){
				message.error('请求失败',3);
			}
		})
	}
	startChange(date, dateString) {
		this.setState({
			startTime: date
		})
	}
	endChange(date, dateString) {
		this.setState({
			endTime: date
		})
	}
	jump(page){
		let _this = this
		this.setState({loading:true,changePage:page})
		let _data = pub.behavior({
			start:page,
			startTime:this.state.status?this.state.startTime.format('YYYY/MM/DD'):'',
			endTime:this.state.status?this.state.endTime.format('YYYY/MM/DD'):'',
			username:this.state.status?this.refs.userName.value:''	
		})
		$.ajax({
			type:'POST',
			url:this.state.status?Urls.FUZZYQUERY:Urls.USERINTGRAL,
			data:_data,
			success:function(data){
				_this.setState({loading:false})
				_this.setState({listUserIntgral:_this.state.status?data.fuzzy?data.fuzzy:[]:data.listUserIntgral?data.listUserIntgral:[]})
			},
			error:function(data){
				message.error('请求失败',3);
			}
		})
	}
	searching(){
		this.setState({loading:true})
		if(!this.state.startTime||!this.state.endTime){
			message.warning("请输入有效时间",3)
			this.setState({loading:false})
			return
		}
		let _this = this;
		let startTime = this.state.startTime.format('YYYY/MM/DD')
		let endTime   = this.state.endTime.format('YYYY/MM/DD')
		// if(this.state.startTime.format()!==null){}
		// this.setState({loading:true})
		// if(startTime ===''|| endTime ===''){
		// 	message.error('请输入时间',3);
		// 	this.setState({loading:false})
		// 	return
		// }
		let data = pub.behavior({
			startTime:startTime,
			endTime:endTime,
			username:this.refs.userName.value?this.refs.userName.value:'',
		})
		$.ajax({
			type:"POST",
			url:Urls.FUZZYQUERY,
			data:data,
			success:function(data){
				//console.log(456,data)
				_this.setState({
					listUserIntgral:data.fuzzy?data.fuzzy:[],
					totalPage:data.total,
					status:true,
					loading:false,
					changePage:1
				})	
			},
			error:function(data){
				message.error('请求失败',3);
				_this.setState({
					loading:false,
				})
			}
		})
	}
	//查询去除特殊符号
	numberInputs=(e)=>{
		let value =e.target.value;
		e.target.value=value.replace(/[@#%&{}?/><[()（）【】{}《》'“‘”’]+/g,'');
	}
	render() {
		return (
			<div className="container">
			    <div className="common-title">会员积分管理</div>
			        <div className="row">
					    <div className="row bg-FFFFFF box-shadow">
					    	<div className="select">
					    		<label>积分变动：</label>
					    	{/*<input type="text" id="start-time" className="form-date" placeholder="年/月/日" ref='startTime'/>*/}
					    		<DatePicker onChange={this.startChange.bind(this)} defaultValue={this.state.startTime}   />
					    		<span className="line-1"></span>
					    	{/*<input type="text" id="end-time" className="form-date" placeholder="年/月/日" ref='endTime'/>*/}
					    		<DatePicker onChange={this.endChange.bind(this)} defaultValue={this.state.endTime}   />
					    		<label>会员名称：</label>
					    		<input type="text" placeholder="会员名称" ref='userName' onKeyUp={this.numberInputs}/>
					    		<a className="btn btn-blue" onClick={()=>{this.searching()}}>查询</a>
					    	</div>
					    </div>
					    <div className="integral-table bg-FFFFFF box-shadow">
					   <Spin spinning={this.state.loading} tip="加载列表中...">
					       <table>
							<thead>
								<tr>
									<th>会员名称</th>
									<th>会员等级</th>
									<th>机构</th>
									<th>可用积分</th>
									<th>上次変动时间</th>
									<th>操作</th>
								</tr>
							</thead>
							<tbody>
							{this.state.listUserIntgral.map((item,index)=>{
								var sD = new Date(item.integrlTime);
								var sData = ''
								var sMonth =''
								var sHours=''
								var sMinutes=''
								var sSeconds=''
								sD.getDate()>=10?sData=sD.getDate():sData='0'+sD.getDate()
								sD.getMonth()+1>=10?sMonth=sD.getMonth()+1:sMonth='0'+(sD.getMonth()+1)
								sD.getHours()>=10?sHours=sD.getHours():sHours='0'+sD.getHours()
								sD.getHours()>=10?sHours=sD.getHours():sHours='0'+sD.getHours()
								sD.getMinutes()>=10?sMinutes=sD.getMinutes():sMinutes='0'+sD.getMinutes()
								sD.getSeconds()>=10?sSeconds=sD.getSeconds():sSeconds='0'+sD.getSeconds()
								var startTime=sD.getFullYear() + '-' + (sMonth) + '-' + sData + ' ' + sHours + ':' + sMinutes + ':' + sSeconds
								return(
								<tr key={index}>
									<td>{item.user.userRealName}</td>
									<td>{item.user.vipGradName}</td>
									<td>{item.user.branchOffice}</td>
									<td>{item.cti}</td>
									<td>{startTime}</td>
									<td><Link to={'/admin/PointDetail/'+item.userName} className="btn btn-blue">查看</Link></td>
								</tr>
									)
							})}
							</tbody>
							</table>
						</Spin>
					    </div>
					</div>
					<div className="line"></div>
					<Pages totalPage={this.state.totalPage} changePage={this.state.changePage} jump={this.jump.bind(this)}/>
			</div>

		)
	}

}
export default PointManage

